package com.itheima.service.iml;

import com.itheima.bean.Employee;
import com.itheima.bean.PageBean;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dao.EmployeeDao;
import com.itheima.exception.CustomException;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author tang
 * @date 2022/11/21 16:40
 */
@Slf4j
@Service
public class EmployeeServiceIml implements EmployeeService {
    @Autowired
    EmployeeDao employeeDao;

    @Override
    public R<Employee> login(String username, String password, HttpSession session) {
        Employee employee = employeeDao.login(username);
        //判断用户是否存在
        if (employee == null) {
            throw new CustomException("用户不存在");
        }
        //判断密码是否正确
        String encode = MD5Encoder.encode(password.getBytes());
        if (employee.getPassword().equals(encode)) {
            throw new CustomException("密码密码错误");
        }
        //判断用户状态是否为启用
        if (employee.getStatus() == 0) {
            throw new CustomException("用户状态为禁用");
        }
        session.setAttribute("employee", employee.getId());

        return R.success(employee);
    }

    @Override
    public R<String> add(Employee employee) {
        //判断账号是否重复
        Integer index = employeeDao.slectCount(employee.getUsername());
        if (index > 0) {
            throw new CustomException("账号不可用");
        }
        Long currentId = BaseContext.getCurrentId();

        employeeDao.add(employee);
        return R.success("添加成功");
    }

    @Override
    public R<PageBean<Employee>> findPage(Integer page, Integer pageSize, String name) {
        //计算页码
        page = (page - 1) * pageSize;
        //查询分页数据
        List<Employee> pageList = employeeDao.findPage(page, pageSize, name);
        //查询分页总记录数
        Integer count = employeeDao.findCount(page, pageSize, name);
        //封装数据到PageBean<Employee>
        PageBean<Employee> pageBean = new PageBean<>(pageList, count, name);

        return R.success(pageBean);
    }

    @Override
    public R<Employee> queryEmployeeById(Long id) {
        Employee employee = employeeDao.queryEmployeeById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("查询数据失败");
    }

    @Override
    public R<String> editEmployee(Employee employee) {
        //查询除自己以外是否有相同的字段
        Integer count = employeeDao.queryEmployeeByIdAndUserName(employee.getUsername(), employee.getId());

        log.info("count{}", count);
        if (employee.getUsername() != null && count > 0) {
            throw new CustomException("员工用户名[" + employee.getUsername() + "]已存在");
        }
        //调用dao层查询数据库
        Integer index = employeeDao.editEmployee(employee);
        if (index > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


}
